package com.android.pagemarket.ui.activity.query;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.android.pagemarket.R;
import com.android.pagemarket.business.logic.InitManager;
import com.android.pagemarket.io.protocol.in.HttpAction.ActionListener;
import com.android.pagemarket.io.protocol.in.TransQueryAction;
import com.android.pagemarket.io.protocol.out.QueryList;
import com.android.pagemarket.ui.activity.base.TemplateActivity;
import com.android.pagemarket.ui.activity.main.LoginActivity;
import com.android.pagemarket.ui.adapter.OrderListAdapter;
import com.android.pagemarket.ui.widget.LoadableView;
import com.android.pagemarket.ui.widget.LoginPopWindow;
import com.android.pagemarket.ui.widget.OrderItemView;
import com.android.pagemarket.ui.widget.TitleBar;
import com.android.pagemarket.ui.widget.TransQueryPopDialog;
import com.android.pagemarket.ui.widget.TransQueryPopWindow;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-11-2
 * @see : 物流查询视图
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class TransQueryActivity extends TemplateActivity implements OnClickListener
{   
    public static final int RID = 0x02;
    private ListView     mListView;
    private OrderListAdapter mListAdapter;
    private TransQueryAction mAction;
    private OrderItemView list_header;
    private LoginPopWindow  loginPopwin;
    private TransQueryPopDialog  queryPopwin;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_transquery);
        initViews();       
    }
    
    @Override
    protected void setupTitleBar(TitleBar titleBar)
    {
        super.setupTitleBar(titleBar);
        titleBar.setTitle(R.string.title_transquery);
    }
   
    @Override
    protected void onResume()
    {
        super.onResume();
        if(!InitManager.getInstance().isLogin())
        {
            showLoginPopWin();
        }else
        {
            load();
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == RID)
        {
            if(resultCode == RESULT_OK && data != null)
            {
                boolean isLogin = data.getBooleanExtra("isLogin",false);
                if(!isLogin)
                {
                    finish();
                }
            }
        }
    }
    
    @Override
    public void refreshView()
    {
        if(mAction != null)
        {
            mListAdapter.reloadAction();
        }
    }
    
    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.btn_query:
            {
                // 查询
                load();
                break;
            }
            case R.id.btn_reSearch:
            {
                // 重新搜索
                showSearchPopWin();
                break;
            }
            case R.id.btn_allorder:
            {
                // 所有订单
                load();
                break;
            }
        }
    }
    
    private void initViews()
    {        
        mListView    = getViewById(R.id.listView);
        list_header  = getViewById(R.id.list_header);
        
        mListAdapter = new OrderListAdapter(this, mListView);
        mListView.setAdapter(mListAdapter);
        mListView.setDivider(getResources().getDrawable(R.drawable.ic_divder));
        
        list_header.setHeadable(true);
        
        updateLoginView();
    }
    
    private void updateLoginView()
    {              
        if(InitManager.getInstance().isLogin())
        {
            TextView com_num = getViewById(R.id.com_num);
            TextView com_name = getViewById(R.id.com_name);
            com_num.setText(InitManager.getInstance().mLoginCompany.companyNum);
            com_name.setText(InitManager.getInstance().mLoginCompany.companyName);
        }
    }
      
    private void showSearchPopWin() 
    {
        /**
        if(queryPopwin == null)
        {
            queryPopwin = new TransQueryPopWindow(this);
        }
        queryPopwin.setAction(mAction,mLoadingView);
        setPopWin(queryPopwin);
        getPopupWin().showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
        */
        if(mAction == null) return;
        if(queryPopwin == null)
        {
            queryPopwin = TransQueryPopDialog.showDialog(this);
        }
        queryPopwin.setAction(mAction,mListAdapter);
        showDialog(queryPopwin);
    }
   
    private void showLoginPopWin()
    {
        LoginActivity.startActivityForResult(this,RID);
        /**
        mTitleBar.postDelayed(new Runnable()
        {            
            @Override
            public void run()
            {
                if(loginPopwin == null)
                {
                    loginPopwin = new LoginPopWindow(TransQueryActivity.this);
                }
                setPopWin(loginPopwin);
                getPopupWin().setOnDismissListener(new OnDismissListener()
                {
                    @Override
                    public void onDismiss()
                    {
                        if (!InitManager.getInstance().isLogin())
                        {
                            finish();
                        } else
                        {
                            updateLoginView();
                            load();
                        }
                    }
                });
                getPopupWin().showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);               
            }
        }, 200);*/
    }
    
    private void load()
    {
        if(!InitManager.getInstance().isLogin()) return;
        updateLoginView();
        mAction = mListAdapter.loadData();
    }
    
    public static void startActivity(Context context)
    {
        Intent it = new Intent(context, TransQueryActivity.class);
        
        context.startActivity(it);
    }
}
